/*
 *  Copyright 2010 Kevin Gaudin
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.tigerlee.candle.spreadsheet;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import android.net.Uri;
import android.util.Log;
/**
 * This class 
 * 
 * @author Kevin Gaudin
 * 
 */
public class GoogleFormSender{

    private final Uri mFormUri;
    Vector<Vector<String>> mParamVector = new Vector<Vector<String>>();
    private static String TAG = "CandleClient-GoogleFormSender";
    /**
     * Creates a new GoogleFormSender which will send data to a Form identified by its key.
     * @param formKey The key of the form. The key is the formKey parameter value in the Form Url: https://spreadsheets.google.com/viewform?formkey=<b>dDN6NDdnN2I2aWU1SW5XNmNyWVljWmc6MQ</b>
     */
    public GoogleFormSender(String url) {
        mFormUri = Uri.parse(url);
    }
    
    public boolean send(Vector<String> mValueVector){
    	
    	//createParamVector(mValueVector);
    	
        final Map<String, String> formParams = remap(mValueVector);
        // values observed in the GoogleDocs original html form
        formParams.put("pageNumber", "0");
        formParams.put("backupCache", "");
        formParams.put("submit", "Envoyer");

        try {
            final URL reportUrl = new URL(mFormUri.toString());
            Log.d(TAG, "Connect to " + reportUrl);

            final MyHttpRequest request = new MyHttpRequest();
            request.sendPost(reportUrl, formParams);

        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    
	private Map<String, String> remap(Vector<String> report) {
        final Map<String, String> result = new HashMap<String, String>();
        for(int i = 0; i < report.size(); i++){
        	result.put("entry." + i + ".single", report.get(i));
        }        
        return result;
    }
}
